package au.edu.anu.club.cs.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "ANUIB";
	private static final int DATABASE_VERSION = 1;
	
	public static final String TABLE_ANUIB_TEAMS = "ANUIB_Teams";
	public static final String TABLE_ANUIB_TEAMLOCATIONS = "ANUIB_TeamLocations";
	
	public static final String TEAMID = "TeamID";
	public static final String TEAMNAME = "TeamName";
	public static final String TEAMLOGO = "TeamLogo";
	public static final String TIMEREGISTERED = "TimeRegistered";
	public static final String LATITUDE = "Latitude";
	public static final String LONGITUDE = "Longitude";
	public static final String TIME = "Time";
	
	private static final String DATABASE_CREATE = ""
			+ "CREATE TABLE " + TABLE_ANUIB_TEAMS
			+ " ( " + "TeamID INTEGER PRIMARY KEY, "
			+ "TeamName TEXT NOT NULL, "
			+ "TeamLogo TEXT, "
			+ "TimeRegistered TEXT NOT NULL "
			+ "); "
			+ "CREATE TABLE " + TABLE_ANUIB_TEAMLOCATIONS
			+ " ( " + "TeamID INTEGER PRIMARY KEY, "
			+ "Latitude TEXT NOT NULL, "
			+ "Longitude TEXT NOT NULL, "
			+ "Time TEXT NOT NULL "
			+ ");";
	
	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MySQLiteHelper.class.getName(), 
				"Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANUIB_TEAMS + ";"
				+ " DROP TABLE IF EXISTS " + TABLE_ANUIB_TEAMLOCATIONS + ";");
		onCreate(db);
	}
	
}
